Repository navigation
Check a type once per write, so a nested expression body runs twice, not depth + 3 times [patch] - #205
Open
matt-edmondson wants to merge 2 commits into
Open
Check a type once per write, so a nested expression body runs twice, not depth + 3 times [patch]#205matt-edmondson wants to merge 2 commits into
matt-edmondson wants to merge 2 commits into
Conversation
…not depth + 3 times [patch] ClassTemplate.WriteTo validated its whole subtree, and every nested type's WriteTo validated its own subtree again; AddSourceFile and AddClasses each validated all types once more. A property's ExpressionBodyFactory ran depth + 3 times, so a stateful factory wrote different code depending on how deeply its class sat. A template write now opens a scope on the CodeBlocker that records which types have been checked, by reference. A type already checked in the scope is not checked again, so the callback runs once to check and once to write. A different type that a callback writes is still checked. Fixes #202 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW5YjyP4TWYsDuKLv95
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW5YjyP4TWYsDuKLv95
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #202
What was wrong
A property's
ExpressionBodyFactoryis rendered once to check it and once to write it. The check itself repeated at every level:ClassTemplate.WriteTovalidated its whole subtree, and then each nested type'sWriteTovalidated its own subtree again.AddSourceFilevalidated every type, thenAddClassesvalidated them again.At depth 3 the callback ran 6 times. A stateful factory, such as a counter for unique names, wrote different code depending on how deeply its class was nested.
Change
A template write now opens a scope on the
CodeBlocker(BeginTemplateWrite, internal). The scope records whichClassTemplates have been checked, by reference.ClassTemplate.WriteTo,ClassTemplate.Validate(for nested types),AddClassesandAddSourceFileskip a type that was already checked in the scope. The set is cleared when the outermost write ends, whether or not it succeeded.ReferenceEqualityComparerdoesn't exist on netstandard2.0/2.1. A different type that aBodyFactorywrites into the sameCodeBlockeris still checked.Tests
ANestedPropertysExpressionBodyRunsOnceToCheckAndOnceToWrite. Depth 3 throughAddSourceFile,ClassTemplate.WriteToandAddClasses. Each expects 2 calls. Before the fix: 6.AStatefulExpressionBodyWritesTheSameCodeWhereverItsClassIsNested. This is the issue's repro: standalone and nested-in-a-file now both writeFirst => 2/Second => 3. Before, the nested case wrote 8 / 9.AClassWrittenFromInsideABodyFactoryIsStillChecked. An invalid type written from a method body callback still throws. It passes before and after, and keeps the skip from widening.This doesn't touch #176 (throwing BodyFactories leave half-written members). That needs the larger pre-render restructure.
Generated by Claude Code